package com.zzq.my.jetpacks.jectpackapp.adapter

import android.text.Editable
import android.text.TextWatcher
import android.view.View.OnFocusChangeListener
import android.widget.EditText
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zzq.my.jetpacks.jectpackapp.R
import com.zzq.my.jetpacks.jectpackapp.bean.MulitEditItemInfo

/**
 * 描述：RecycleView使用多item编辑问题 EditText
 * 作者：zhuangzeqin
 * 时间: 2022/1/19-14:16
 * 邮箱：zzq@eeepay.cn
 * 备注:
 */
class MulitEditsAdapter : BaseQuickAdapter<MulitEditItemInfo, BaseViewHolder>(R.layout.item_mulit_edit) {
    /**
     * Implement this method and use the helper to adapt the view to the given item.
     *
     * 实现此方法，并使用 helper 完成 item 视图的操作
     *
     * @param holder A fully initialized helper.
     * @param item   The item that needs to be displayed.
     */
    override fun convert(holder: BaseViewHolder, item: MulitEditItemInfo) {
        holder.setText(R.id.tv_num, "序号：" + item.id)
        holder.setText(R.id.tv_name, "姓名：" + item.name)
        //年龄
        holder.getView<EditText>(R.id.et_age).setText(item.age.toString())
        //地址
        holder.getView<EditText>(R.id.et_address).setText(item.address)
        editSetOnFocusChangeListener(holder.getView<EditText>(R.id.et_age),holder.layoutPosition,0);
        editSetOnFocusChangeListener(holder.getView<EditText>(R.id.et_address),holder.layoutPosition,1);

    }

    //以下代码为重点
    private fun editSetOnFocusChangeListener(edt: EditText, position: Int, i: Int) {
        val textWatcher: TextWatcher = object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                //删除.后面超过两位的数字
                var s = s
                if (s.toString().contains(".")) {
                    if (s.length - 1 - s.toString().indexOf(".") > 2) {
                        s = s.toString().subSequence(
                            0,
                            s.toString().indexOf(".") + 3
                        )
                        edt.setText(s)
                        edt.setSelection(s.length)
                    }
                }
                //如果.在起始位置,则起始位置自动补0
                if (s.toString().trim { it <= ' ' }.substring(0) == ".") {
                    s = "0$s"
                    edt.setText(s)
                    edt.setSelection(2)
                }
                //如果起始位置为0并且第二位跟的不是".",则无法后续输入
                if (s.toString().startsWith("0")
                    && s.toString().trim { it <= ' ' }.length > 1
                ) {
                    if (s.toString().substring(1, 2) != ".") {
                        edt.setText(s.subSequence(0, 1))
                        edt.setSelection(1)
                        return
                    }
                }
            }

            override fun afterTextChanged(s: Editable) {
                when (i) {
                    0 -> if (s.isNotEmpty()) {
                        data[position].age = s.toString().toDoubleOrNull()
                    } else {
                        data[position].age = 0.00
                    }
                    1 -> if (s.isNotEmpty()) {
                        data[position].address = s.toString()
                    } else {
                        data[position].address = ""
                    }
                }
            }
        }
        edt.onFocusChangeListener = OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                edt.addTextChangedListener(textWatcher)
            } else {
                edt.removeTextChangedListener(textWatcher)
            }
        }
    }

}